<template>
  <div class="flowpath-container">
    <div>
      <div class="mb20">
        <span class="color_494949 ft12 pr5 w70 ml10">{{ translate('品类编码') }}：</span>
        <el-input
          v-model="searchInfo1"
          class="w192 mr10"
          :placeholder="translate('请输入关键字')"
        />
        <el-button class="settingBtn ml10" type="primary" @click="getList">
          {{ translate('查询') }}
        </el-button>
        <!-- <el-button class="settingBtn mr10" type="primary" @click="resetFn">
          {{ translate('重置') }}
        </el-button> -->
      </div>
      <div class="add">
        <el-button
          v-if="MenuAuths('add')"
          class="settingBtn"
          type="primary"
          @click="addFn"
          plain
        >
          {{ translate('新增') }}
        </el-button>
         <el-upload
          accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
          :action="action"
          :before-upload="beforeUpload"
          class="ml-upload"
          :headers="headers"
          name="fileInput"
          :on-error="handleError"
          :on-success="handleSuccess"
          :show-file-list="false"
        >
          <el-button type="primary" @click="uploadFn()" plain>
            {{ translate('导入') }}
          </el-button>
        </el-upload>
        <el-button
          v-if="MenuAuths('delete')"
          class="settingBtn"
          type="primary"
          @click="batchdeletedFn"
          plain
        >
          {{ translate('批量删除') }}
        </el-button>
      </div>
      <el-table
          :data="tableData"
          v-loading="loading"
          :row-class-name="tableRowClassName"
          :row-key="getRowKey"
          @selection-change="handleSelectionChange"
          >
        <el-table-column prop="value" type="selection" width="55" />
        <el-table-column :label="translate('大类')" prop="LCategory" />
        <el-table-column :label="translate('大类名称')">
          <template #default="{ row }">
            <div>
               {{ row.LCategoryName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="translate('中类')" prop="MCategory" />
        <el-table-column :label="translate('中类名称')" prop="MCategoryName" />
        <el-table-column
          :label="translate('小类')"
          prop="SCategory"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('小类名称')"
          prop="SCategoryName"
          show-overflow-tooltip
        />
        <el-table-column :label="translate('品类编码')">
          <template #default="{ row }">
            <div>
              <span class="iconcolor">{{ row.CategoryCode }}</span>
              <vab-icon icon="file-copy-line" @click="copyToClipboard(row.CategoryCode)" />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          :label="translate('品类名称')"
          prop="CategoryName"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('基本计量单位编码')"
          prop="BaseUnitCode"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('基本计量单位描述')"
          prop="BaseUnitRemark"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('物料拼接规则')"
          prop="ItemRule"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性1编码')"
          prop="AttributeCode1"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性1名称')"
          prop="AttributeName1"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性2编码')"
          prop="AttributeCode2"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性2名称')"
          prop="AttributeName1"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性3编码')"
          prop="AttributeCode3"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性3名称')"
          prop="AttributeName3"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性4编码')"
          prop="AttributeCode4"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性4名称')"
          prop="AttributeName4"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性5编码')"
          prop="AttributeCode5"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性5名称')"
          prop="AttributeName5"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性6编码')"
          prop="AttributeCode3"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性6名称')"
          prop="AttributeName3"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性7编码')"
          prop="AttributeCode7"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性7名称')"
          prop="AttributeName7"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性8编码')"
          prop="AttributeCode8"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性8名称')"
          prop="AttributeName8"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性9编码')"
          prop="AttributeCode9"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性9名称')"
          prop="AttributeName9"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性10编码')"
          prop="AttributeCode10"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性10名称')"
          prop="AttributeName10"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性11编码')"
          prop="AttributeCode11"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性11名称')"
          prop="AttributeName11"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性12编码')"
          prop="AttributeCode12"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性12名称')"
          prop="AttributeName12"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性13编码')"
          prop="AttributeCode13"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性13名称')"
          prop="AttributeName13"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性14编码')"
          prop="AttributeCode14"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性14名称')"
          prop="AttributeName14"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性15编码')"
          prop="AttributeCode15"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('特性15名称')"
          prop="AttributeName15"
          show-overflow-tooltip
        />
        <el-table-column :label="translate('操作')" width="200">
          <template #default="{ row }">
            <!-- <el-button plain size="small" type="primary" @click="examineFn(row)">
              {{ translate('审核') }}
            </el-button> -->
            <el-button
              v-if="MenuAuths('edit')"
              plain
              size="small"
              type="primary"
              @click="detailFn(row)"
            >
              {{ translate('编辑') }}
            </el-button>
            <!-- <el-button plain size="small" type="primary" @click="printFn(row)" :disabled="row.BillStatus==='作废'?true:false">
              {{ translate('打印') }}
            </el-button>
            <el-button plain size="small" type="primary" @click="ZFFn(row)">
              {{ translate('作废') }}
            </el-button> -->
            <el-button
              v-if="MenuAuths('delete')"
              plain
              size="small"
              type="danger"
              @click="deletedFn(row)"
            >
              {{ translate('删除') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          :current-page="pageNo"
          :layout="layout"
          :page-size="pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-dialog
      v-model="dialogVisible"
      :title="translate('物料分类与属性规范详情')"
      width="1340px"
      @close="closeFn"
      :close-on-click-modal="false"
    >
      <detail
        :form="form"
        fromtype="goodslist"
      />
      <template #footer>
        <el-button @click="dialogVisible = false">
          {{ translate('取消') }}
        </el-button>
        <el-button :disabled="isclick" type="primary" @click="submit">
          {{ translate('确定') }}
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import {
    WmsItemCodeData,
    DelItemCodeApply,
    WmsItemCodeAdd,
    BatchDelItemCodeApply
  } from '@/api/form'
  import detail from './components/detail'
  import { MenuAuth } from '~/src/utils/permission'
  import VabPrint from '@/plugins/VabPrint'
  import { translate } from '@/i18n'
  import { parseTime } from '@/utils'
  import { baseURL } from '@/config'
  import { useUserStore } from '@/store/modules/user'
  const userStore = useUserStore()
  const { token, accountInfo } = useUserStore()
  const { realname } = storeToRefs(userStore)
  export default defineComponent({
    name: 'Flowpath',
    components: { detail },
    data() {
      return {
        tableData: [],
        searchInfo: '',
        searchInfo1: '',
        searchInfo3: '',
        wheres: '',
        total: 0,
        pageNo: 1,
        pageSize: 20,
        dialogVisible: false,
        enabled: 0,
        action: '',
        headers: '',
        DetailPageData: {},
        chiledPrintdetail: [],
        isclick: false,
        submitType: 'add',
        layout: 'total, sizes, prev, pager, next, jumper',
        deleList: [],
        MenuAuths: '',
        tableRef: null,
        isPrint: false,
        loading: true,
        isdrawershow: false,
        nodeList: {},
        formId: '',
        status: -1,
        PBMID: '',
        selectArr: [],
        form: {
          LCategory:'',
          LCategoryName: '',
          MCategory: '',
          MCategoryName: '',
          SCategory: '',
          SCategoryName: '',
          CategoryCode: '',
          CategoryName: '',
          BaseUnitCode: '',
          BaseUnitRemark: '',
          ItemRule: '',
          AttributeCode1: '',
          AttributeName1: '',
          AttributeCode2: '',
          AttributeName2: '',
          AttributeCode3: '',
          AttributeName3: '',
          AttributeCode4: '',
          AttributeName4: '',
          AttributeCode5: '',
          AttributeName5: '',
          AttributeCode6: '',
          AttributeName6: '',
          AttributeCode7: '',
          AttributeName7: '',
          AttributeCode8: '',
          AttributeName8: '',
          AttributeCode9: '',
          AttributeName9: '',
          AttributeCode10: '',
          AttributeName10: '',
          AttributeCode11: '',
          AttributeName11: '',
          AttributeCode12: '',
          AttributeName12: '',
          AttributeCode13: '',
          AttributeName13: '',
          AttributeCode14: '',
          AttributeName14: '',
          AttributeCode15: '',
          AttributeName15: '',
        },
        rules: {
          code: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入工号'),
            },
          ],
          Account: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入登录账号'),
            },
          ],
          workFlowName: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入账号名称'),
            },
          ],
        },
      }
    },
    created() {
      this.MenuAuths = MenuAuth
      this.getList()
    },
    methods: {
      tableRowClassName({ row, rowIndex }) {
        row.row_index = rowIndex
      },
      handleSelectionChange(e) {
        this.selectArr = e
      },
      getRowKey(row) {
        return row.PBMID
      },
      resetFn() {
        this.searchInfo1 = ''
        this.pageNo = 1
        this.pageSize = 20
        this.getList()
      },
      async ZFFn(row) {
        this.formId = row.PBMID
          this.$router.push({
            path: '/table/detail',
            query: {
              formId: this.formId,
              formcode: 'Wms_PickingBillMaster',
              type: 'zf',
              statusSH: row.BillStatus
            },
        })
      },
      //复制标识
      async copyToClipboard(val) {
        if (navigator.clipboard && window.isSecureContext) {
          try {
          console.log(navigator.clipboard)
          console.log(window.isSecureContext)
          await navigator.clipboard.writeText(val)
          this.$message({
            message:'复制成功',
            type:'success',
          })
          } catch (err) {
            this.$message({
            message:'复制失败',
            type:'error',
          })
          }

        }else {
          // 创建text area
        const textArea = document.createElement("textarea")
        textArea.value = val
        // 使text area不在viewport，同时设置不可见
        textArea.style.position = "absolute"
        textArea.style.opacity = 0
        textArea.style.left = "-999999px"
        textArea.style.top = "-999999px"
        document.body.appendChild(textArea)
        textArea.focus()
        textArea.select()

        return new Promise((res, rej) => {
          // 执行复制命令并移除文本框
          document.execCommand("copy") ? res() : rej()
          textArea.remove()
        }).then(() => {
          this.$message({
              message: "复制成功",
              type: "success",
            })
          }).catch((err) => {
            this.$message({
              message: "复制失败",
              type: "error",
            })
          })
        }

      },
      translate(row) {
        return translate(row)
      },
      handleClose() {
        this.isdrawershow = false
      },
      async submitPrint() {
        document.title = '领料单'
        await VabPrint(this.$refs.tableRef)
        this.isPrint = false
      },
      async getList() {
        this.wheres = ''
        if (this.searchInfo1 ) {
          const arr = []
          this.searchInfo1.trim() !== '' &&
            arr.push({
              name: 'CategoryCode',
              value: this.searchInfo1,
              displayType: 'like',
            })
          this.wheres = JSON.stringify(arr)
        }
        const params = {
          pageNo: this.pageNo,
          pageSize: this.pageSize,
          tableName: '',
          sort: '',
          order: '',
          wheres: this.wheres,
          export: false,
          value: '',
          openid: ''
        }
        const data = await WmsItemCodeData(params)
        this.tableData = data.data.rows
        this.total = data.data.total
        console.log(this.tableData)
        this.loading = false
      },
      async uploadFn() {
        this.headers = this.headers = {
          Authorization: `Bearer ${token}`,
        }
        this.action = `${baseURL}/Wms_ItemCode_Apply/Import`
      },
      handleError(data) {
        console.log(data)
      },
      handleSuccess(data) {
        console.log(data)
        if (data.code === '200') {
          this.$message({
            type: 'success',
            message: data.message,
          })
          this.getList()
        } else {
          this.$message({
            message: data.message,
            type: 'error',
          })
        }
      },
      async deletedFn(row) {
        this.$confirm(translate('确认删除该吗?'), translate('提示'), {
          confirmButtonText: translate('确定'),
          cancelButtonText: translate('取消'),
          type: 'warning',
        })
          .then(() => {
            this.deleted(row)
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: translate('已取消删除'),
            })
          })
      },
      async batchdeletedFn(row) {
        if(this.selectArr.length<=0){
          this.$message({
            message: translate('请选择要删除的数据'),
            type: 'warning',
          })
          return
        }
        this.$confirm(translate('确认删除选中数据吗?'), translate('提示'), {
          confirmButtonText: translate('确定'),
          cancelButtonText: translate('取消'),
          type: 'warning',
        })
          .then(() => {
            this.bacthdeleted(row)
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: translate('已取消删除'),
            })
          })
      },
      async bacthdeleted() {
        const icaidArr = []
        this.selectArr.map((item,index) =>{
          icaidArr.push(
              {
                icaid: item.ICAID,
              }
             )
        })
        const params = icaidArr
        const data = await BatchDelItemCodeApply(params)
        if (data.code == '200') {
          this.$message({
            message: translate('删除成功'),
            type: 'success',
          })
          this.loading = true
          await this.getList()
        }
      },
      async deleted(row) {
        const params = {
              icaid: row.ICAID
        }
        const data = await DelItemCodeApply(params)
        if (data.code == '200') {
          this.$message({
            message: translate('删除成功'),
            type: 'success',
          })
          this.loading = true
          await this.getList()
        }
      },
      //分页条数切换
      handleSizeChange(val) {
        this.pageSize = val
        this.getList()
      },
      //分页切换
      handleCurrentChange(val) {
        this.pageNo = val
        this.getList()
      },
      async addFn() {
        this.submitType = 'add'
        this.status = -1
        console.log(userStore.accountInfo.departmentName)
        this.form = {
          PickingCode: '',
          PickingNo: '',
          PickingUnit: userStore.accountInfo.departmentName,
          PickingName: '',
          SendUnit: '',
          PickingDate: '',
          AccountVoucher: '',
          PickingPerson: userStore.realname,
          Contacts: '',
          ContactsTel: '',
          DeliveryDate: '',
          IsDelivery: false,
          Custodian: '',
          Remark: '',
        }
        this.dialogVisible = true
      },
      submit() {
          this.AddData()
      },
      closeFn() {
        this.enabled = 0
        this.form.WorkFlowID = ''
      },
      async detailFn(row) {
        this.deleList = []
        this.form = row
        this.PBMID = row.PBMID
        this.submitType = 'edit'
        this.dialogVisible = true
      },
      async AddData() {
        this.isclick = true
        const params = this.form
        const data = await WmsItemCodeAdd(params)
        if (data.code == '200') {
          this.getList()
          this.$message({
            message: translate('保存成功'),
            type: 'success',
          })
          this.dialogVisible = false
        }
        setTimeout(() => {
          this.isclick = false
        }, 5000)
      },
    },
  })
</script>
<style lang="scss" scoped>
  .flowpath-container {
    .color_494949 {
      color: #494949;
    }
    .ml-upload {
      display: inline-block;
      margin-left: 10px;
      vertical-align: bottom;
      margin-right: 10px;
    }
    .w192 {
      width: 192px;
    }
    .mb20 {
      margin-bottom: 20px;
    }
    .mr10 {
      margin-right: 10px;
    }
    .ml10 {
      margin-left: 10px;
    }
    .add {
      margin-bottom: 10px;
      text-align: left;
    }

    .iconcommon {
      position: absolute;
      top: 80px;
      right: 70px;
      font-size: 60px;
      color: blue;
    }
    .iconcommon2 {
      position: absolute;
      top: 80px;
      right: 70px;
      font-size: 60px;
      color: red;
    }
    .w70 {
      display: inline-block;
      width: 70px;
      text-align: right;
    }
    .condition-container {
      display: inline-block;
      margin-top: 10px;
    }
    .iconcolor {
      color: #409eff;
    }
    .color_l {
      color: #67c23a;
    }
    .color_red {
      color: #f56c6c;
    }
  }
</style>
